package cn.ocean.org.controller;

import cn.ocean.basic.utils.AjaxResult;
import cn.ocean.basic.utils.PageList;
import cn.ocean.org.domain.Department;
import cn.ocean.org.query.DepartmentQuery;
import cn.ocean.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 部门相关的curd功能
* @Author: Fill
* @Version: 1.0
* @Date:  2022/6/24 11:58
*/

// 复合注解
@RestController
@RequestMapping("/department")
@Api(value = "部门的Api", description = "部门相关的curd功能")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;
   /**
   * @Title: getById
   * @Description:根据Id查询
   * @Author: Fill
   * @Version: 1.0
   * @Date:  2022/6/24 11:55
   * @Parameters: [id]
   * @Return cn.ocean.org.domain.Department
   */
    @ApiOperation(value = "通过id的查询")
    @GetMapping("/{id}")
    public Department getById(@PathVariable("id") Long id){
        return departmentService.getByIdDep(id);
    }
    /**
    * @Title: queryData
    * @Description: 高级查询加分页查询接口
    * @Author: Fill
    * @Version: 1.0
    * @Date:  2022/6/24 11:55
    * @Parameters: [departmentQuery]
    * @Return cn.ocean.basic.utils.PageList<cn.ocean.org.domain.Department>
    */
    @ApiOperation(value = "高级查询和分页")
    @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryData(departmentQuery);

    }
   /**
   * @Title: delDept
   * @Description: 删除方法
   * @Author: Fill
   * @Version: 1.0
   * @Date:  2022/6/24 11:55
   * @Parameters: [id]
   * @Return cn.ocean.basic.utils.AjaxResult
   */
   @ApiOperation(value = "根据id删除")
    @DeleteMapping("{id}")
    public AjaxResult delDept(@PathVariable("id") Long id){
        try {
            departmentService.delDep(id);
            return AjaxResult.getAjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMsg("系统繁忙，请稍后重试");
        }
    }

   /**
   * @Title: addOrUptDept
   * @Description: 新增或修改
   * @Author: Fill
   * @Version: 1.0
   * @Date:  2022/6/24 11:57
   * @Parameters: [department]
   * @Return cn.ocean.basic.utils.AjaxResult
   */
    @ApiOperation(value = "新增或修改")
    @PutMapping
    public AjaxResult addOrUptDept(@RequestBody Department department){
        try{
            if (department.getId() == null){
                departmentService.addDep(department);

            }else {
                departmentService.updateDep(department);
            }

            return AjaxResult.getAjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMsg("系统繁忙，请稍后重试！！！");
        }
    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Fill
    * @Version: 1.0
    * @Date:  2022/6/24 11:59
    * @Parameters: []
    * @Return java.util.List<cn.ocean.org.domain.Department>
    */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Department> loadAll(){
        return departmentService.loadAllDep();

    }


}
